﻿Imports Publics
Imports System.Windows.Forms

Public Class FrmDMNhomVatTu
    Private _IDNhomVatTu As String
    Public Property IDNhomVatTu() As String
        Get
            Return _IDNhomVatTu
        End Get
        Set(ByVal value As String)
            _IDNhomVatTu = value
        End Set
    End Property

    Private _LaChon As Boolean
    Public Property LaChon() As Boolean
        Get
            Return _LaChon
        End Get
        Set(ByVal value As Boolean)
            _LaChon = value
        End Set
    End Property

    Private _MaNhomVatTu As String
    Public Property MaNhomVatTu() As String
        Get
            Return _MaNhomVatTu
        End Get
        Set(ByVal value As String)
            _MaNhomVatTu = value
        End Set
    End Property

    Private _ChonNhomVatTu As Boolean
    Public Property ChonNhomVatTu() As Boolean
        Get
            Return _ChonNhomVatTu
        End Get
        Set(ByVal value As Boolean)
            _ChonNhomVatTu = value
        End Set
    End Property

    Private Sub FrmDMNhomVatTu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
        If e.KeyCode = Keys.Enter And LaChon And grvNhomVatTu.FocusedRowHandle >= 0 Then
            IDNhomVatTu = grvNhomVatTu.GetRowCellValue(grvNhomVatTu.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
        'Thêm mới
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pADD, btnThemMoi) Then
            btnThemMoi_ItemClick(Nothing, Nothing)
        End If
        'Sửa đổi
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEDIT, btnSuaDoi) Then
            btnSuaDoi_ItemClick(Nothing, Nothing)
        End If
        'Xóa
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pDELETE, btnXoa) Then
            btnXoa_ItemClick(Nothing, Nothing)
        End If
        'Xuất
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pEXPORT, btnXuatExcel) Then
            btnXuatExcel_ItemClick(Nothing, Nothing)
        End If
        'Nạp lại
        If clsDSPhimTatDefault.isCheckPhimTat(e, ELoaiPhimTat.pREFRESH, btnNapLai) Then
            btnNapLai_ItemClick(Nothing, Nothing)
        End If
    End Sub
    Public Shared Them, Sua, Xoa, SaoChep As Boolean
    Private Sub PhanQuyen()
        btnThemMoi.Visibility = IIf(Them, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSaoChep.Visibility = IIf(SaoChep, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnSuaDoi.Visibility = IIf(Sua, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
        btnXoa.Visibility = IIf(Xoa, DevExpress.XtraBars.BarItemVisibility.Always, DevExpress.XtraBars.BarItemVisibility.Never)
    End Sub

    Private Sub FrmDMNhomVatTu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnThemMoi.Caption = BtnAddNewCaption
        btnSuaDoi.Caption = BtnEditCaption
        btnXoa.Caption = BtnDeleteCaption
        btnSaoChep.Caption = BtnCopyCaption
        btnThoat.Caption = BtnExitCaption
        btnNapLai.Caption = BtnRefreshCaption
        btnXuatExcel.Caption = BtnExportDataCaption
        If Not LaChon Then
            btnChon.Visibility = DevExpress.XtraBars.BarItemVisibility.Never
        End If
        GroupControl1.Text = GetLabelPhimTatCoBan("", True, True, True)
        FillDataToGrid()
        SetFilterConditionMa(grvNhomVatTu, grdColMaNhomVatTu, grdColMaNhomVatTu.FieldName, MaNhomVatTu, LaChon)
        PhanQuyen()
    End Sub
    Private Sub FillDataToGrid()
        Try
            If LaChon Then
                If ChonNhomVatTu Then
                    Dim lstSource = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.TinhChat.GetValueOrDefault = ETinhChatSanPham.NguyenLieu Order By ds.MaNhom Select New With { _
                            .ID = ds.ID, .MaNhomVatTu = ds.MaNhom, .TenNhomVatTu = ds.TenNhom, .GhiChu = ds.GhiChu, .TinhChatTen = "", .TinhChat = ds.TinhChat}).ToList

                    grdNhomVatTu.DataSource = lstSource
                    grdNhomVatTu.RefreshDataSource()
                Else
                    Dim lstSource = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed And ds.TinhChat.GetValueOrDefault = ETinhChatSanPham.ThucDon Order By ds.MaNhom Select New With { _
                            .ID = ds.ID, .MaNhomVatTu = ds.MaNhom, .TenNhomVatTu = ds.TenNhom, .GhiChu = ds.GhiChu, .TinhChatTen = "", .TinhChat = ds.TinhChat}).ToList
                    For Each item In lstSource
                        If item.TinhChat.GetValueOrDefault = ETinhChatSanPham.NguyenLieu Then
                            item.TinhChatTen = "Nguyên liệu"
                        Else
                            item.TinhChatTen = "Thực đơn"
                        End If
                    Next
                    grdNhomVatTu.DataSource = lstSource
                    grdNhomVatTu.RefreshDataSource()
                End If
            Else
                Dim lstSource = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed Order By ds.MaNhom Select New With { _
                            .ID = ds.ID, .MaNhomVatTu = ds.MaNhom, .TenNhomVatTu = ds.TenNhom, .GhiChu = ds.GhiChu, .TinhChatTen = "", .TinhChat = ds.TinhChat}).ToList
                For Each item In lstSource
                    If item.TinhChat.GetValueOrDefault = ETinhChatSanPham.NguyenLieu Then
                        item.TinhChatTen = "Nguyên liệu"
                    Else
                        item.TinhChatTen = "Thực đơn"
                    End If
                Next
                grdNhomVatTu.DataSource = lstSource
                grdNhomVatTu.RefreshDataSource()
            End If

        Catch ex As Exception
            MsgErr(ex.Message)
        End Try
    End Sub

    Private Sub btnChon_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnChon.ItemClick
        If grvNhomVatTu.FocusedRowHandle >= 0 Then
            IDNhomVatTu = grvNhomVatTu.GetRowCellValue(grvNhomVatTu.FocusedRowHandle, grdColID).ToString
            Me.Close()
        End If
    End Sub

    Private Sub btnThemMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThemMoi.ItemClick
        Dim frm As New FrmCapNhatNhomVatTu
        frm.Action = vAddAction
        frm.StartPosition = Windows.Forms.FormStartPosition.CenterParent
        frm.ShowDialog()
        FillDataToGrid()

    End Sub

    Private Sub btnSaoChep_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSaoChep.ItemClick
        If grvNhomVatTu.FocusedRowHandle < 0 Then
            MsgInfo("Bạn chưa chọn bản ghi để sao chép")
            Return
        End If
        Dim strID As String = grvNhomVatTu.GetRowCellValue(grvNhomVatTu.FocusedRowHandle, grdColID).ToString
        Dim objEdit = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If objEdit IsNot Nothing Then
            Dim frm As New FrmCapNhatNhomVatTu
            frm.Action = vCopyAction
            frm.NhomVatTuEdit = objEdit
            frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        Else
            MsgInfo("Không tồn tại dữ liệu")
        End If
    End Sub

    Private Sub btnSuaDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnSuaDoi.ItemClick
        If grvNhomVatTu.FocusedRowHandle < 0 Then
            MsgInfo("Bạn chưa chọn bản ghi để sửa")
            Return
        End If
        Dim strID As String = grvNhomVatTu.GetRowCellValue(grvNhomVatTu.FocusedRowHandle, grdColID).ToString
        Dim objEdit = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
        If objEdit IsNot Nothing Then
            Dim frm As New FrmCapNhatNhomVatTu
            frm.Action = vEditAction
            frm.NhomVatTuEdit = objEdit
            frm.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
            frm.ShowDialog()
            FillDataToGrid()
        Else
            MsgInfo("Không tồn tại dữ liệu")
        End If
    End Sub

    Private Sub btnXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXoa.ItemClick
        If grvNhomVatTu.FocusedRowHandle > -1 Then
            If MsgAskDelete(String.Format("Bạn có muốn xóa bản ghi {0} này không?", grvNhomVatTu.GetRowCellValue(grvNhomVatTu.FocusedRowHandle, grdColMaNhomVatTu).ToString)) = vMsg.vReSult.Yes Then
                Try
                    Dim strID = grvNhomVatTu.GetRowCellValue(grvNhomVatTu.FocusedRowHandle, grdColID).ToString()
                    Dim objDelete = (From ds In dbTV.VT_DM_NhomVatTus Where ds.InUsed AndAlso ds.ID = strID Select ds).FirstOrDefault
                    If objDelete IsNot Nothing Then
                        objDelete.InUsed = False
                        objDelete.EditedOn = dbTV.GetDateTimeServer
                        iSubmitChange()
                        FillDataToGrid()
                    End If
                Catch ex As Exception
                    MsgInfo(ex.Message)
                End Try
            End If
        End If
    End Sub

    Private Sub btnNapLai_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnNapLai.ItemClick
        FillDataToGrid()
    End Sub

    Private Sub btnXuatExcel_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnXuatExcel.ItemClick
        Export_Dialog(SaveFileDialog1, grdNhomVatTu)
    End Sub

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub
End Class